#include "misc.h"

std::string jsonToString(const Json::Value &x) {
    static Json::StreamWriterBuilder builder;
    builder.settings_["indentation"] = "";
    return Json::writeString(builder, x);
}

Json::Value stringToJson(const std::string &x) {
    Json::Value root;
    static Json::CharReaderBuilder readerBuilder;
    static Json::CharReader* reader = readerBuilder.newCharReader();
    std::string errs;
    reader->parse(x.c_str(),
                 x.c_str() + x.length(),
                 &root, &errs);
    return root;
}

void SplitString(const std::string &s, std::vector<std::string> &v, const std::string &c) {
    std::string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while(std::string::npos != pos2)
    {
        v.push_back(s.substr(pos1, pos2-pos1));

        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if(pos1 != s.length())
        v.push_back(s.substr(pos1));
}
